package com.zuipin.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.shoppingCart.CartParam;
import com.zuipin.entity.shoppingCart.ShoppingCart;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IMemberAccountService;
import com.zuipin.service.IShoppingCartService;
import com.zuipin.util.ConstantsUtil;
import com.zuipin.util.DateUtils;
import com.zuipin.util.RegexUtils;
import com.zuipin.vo.FrontOrderVo;
import com.zuipin.vo.OrderBuyAgainVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @description : 购物车
 * @author : wuhaijun
 * @date : 2017年4月7日 下午3:34:57
 */
@Api(description = "购物车-wuhaijun")
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
	
	@Autowired
	private IShoppingCartService	shoppingCartService;
	
	@Resource
	private IMemberAccountService	memberAccountService;
	
	@RequestMapping(value = "/del01", method = RequestMethod.POST)
	@ApiOperation(value = "删除购物车", notes = "删除购物车")
	@ApiImplicitParams({ @ApiImplicitParam(name = "ids", value = "删除购物的id", required = true, dataType = "String", paramType = "query") })
	public Result delById(@RequestParam String ids) {
		Result result = new Result();
		shoppingCartService.del(ids);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("删除成功");
		return result;
	}
	
	/**
	 * @description : 购物车累加购买数量
	 * @author : wuhaijun
	 * @date : 2017年4月19日 下午3:48:28
	 */
	@ApiOperation(value = "购物车累加购买数量_value", notes = "购物车累加购买数量_notes" + "</br>返回码" + "</br>4062-购买商品未达到最低定购量" + "</br>4063-购买商品超过最高定购量")
	@RequestMapping(value = "/addBuyNum", method = RequestMethod.POST)
	@Valids({ @Valid(name = "buyNum", minValue = 1), @Valid(name = "device", regex = "WAP|PC") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "proId", value = "商品ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "buyerId", value = "买家ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sellerId", value = "卖家ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "buyNum", value = "购买数量", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "packId", value = "泡带ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "packCode", value = "泡带编码", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "packSuttle", value = "泡袋净重", required = false, dataType = "bigDecimal", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "设备/终端", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<Map<String, Object>> addBuyNum(@RequestParam Long proId, @RequestParam Long buyerId, @RequestParam(required = false) Long sellerId, @RequestParam Integer buyNum,
			@RequestParam(required = false) Long packId, @RequestParam(required = false) String packCode, @RequestParam(required = false) BigDecimal packSuttle,
			@RequestParam String device, @RequestParam Long sysId) throws Exception {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		try {
			CartParam cartParam = new CartParam();
			cartParam.setProId(proId);
			cartParam.setBuyerId(buyerId);
			cartParam.setSellerId(sellerId);
			cartParam.setBuyNum(buyNum);
			cartParam.setPackId(packId);
			cartParam.setPackCode(packCode);
			cartParam.setPackSuttle(packSuttle);
			cartParam.setDevice(device);
			cartParam.setSysId(sysId);
			cartParam.setAddTime(DateUtils.getCurrentDateTime());
			Map<String, Object> map = shoppingCartService.addBuyNum(cartParam);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("加入购物车成功");
		} catch (CustomException e) {
			result.setCode(e.getCode());
			result.setMessage(e.getMessage());
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	/**
	 * @description : 购物车更新购买数量
	 * @author : wuhaijun
	 * @date : 2017年4月19日 下午5:42:33
	 */
	@ApiOperation(value = "购物车更新购买数量_value", notes = "购物车更新购买数量_notes </br>  isDisable:true 失效 " + "</br>返回码" + "</br>4062-购买商品未达到最低定购量" + "</br>4063-购买商品超过最高定购量")
	@RequestMapping(value = "/updateBuyNum", method = RequestMethod.POST)
	@Valids({ @Valid(name = "buyNum", minValue = 1), @Valid(name = "device", regex = "WAP|PC") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "购物车ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "proId", value = "商品ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "buyerId", value = "买家ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sellerId", value = "卖家ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "buyNum", value = "购买数量", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "packId", value = "泡带ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "packCode", value = "泡带编码", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "packSuttle", value = "泡袋净重", required = false, dataType = "bigDecimal", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "设备/终端", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<Map<String, Object>> updateBuyNum(@RequestParam Long id, @RequestParam Long proId, @RequestParam Long buyerId, @RequestParam(required = false) Long sellerId,
			@RequestParam Integer buyNum, @RequestParam(required = false) Long packId, @RequestParam(required = false) String packCode,
			@RequestParam(required = false) BigDecimal packSuttle, @RequestParam String device, @RequestParam Long sysId) throws Exception {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		try {
			CartParam cartParam = new CartParam();
			cartParam.setId(id);
			cartParam.setProId(proId);
			cartParam.setBuyerId(buyerId);
			cartParam.setSellerId(sellerId);
			cartParam.setBuyNum(buyNum);
			cartParam.setPackId(packId);
			cartParam.setPackCode(packCode);
			cartParam.setPackSuttle(packSuttle);
			cartParam.setDevice(device);
			cartParam.setSysId(sysId);
			cartParam.setAddTime(DateUtils.getCurrentDateTime());
			Map<String, Object> map = shoppingCartService.updateBuyNum(cartParam);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("更新购物车成功");
			result.setData(map);
		} catch (CustomException e) {
			result.setCode(e.getCode());
			result.setMessage(e.getMessage());
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	/**
	 * @description : 删除购物车
	 * @author : wuhaijun
	 * @date : 2017年4月19日 下午7:11:59
	 */
	@ApiOperation(value = "删除购物车_value", notes = "删除购物车_notes")
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ApiImplicitParams({ @ApiImplicitParam(name = "ids", value = "购物车ID", required = true, dataType = "string", paramType = "query") })
	public Result<Map<String, Object>> del(@RequestParam String ids) {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		shoppingCartService.del(ids);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("删除成功");
		return result;
	}
	
	/**
	 * @description : 购物车查询
	 * @author : wuhaijun
	 * @date : 2017年4月19日 下午6:38:56
	 */
	@ApiOperation(value = "购物车查询_value", notes = "购物车查询_notes" + "</br>新增返回字段：</br>" //
			+ "商品详情</br>" + "isIntegralPro-是否是积分商品 1是 0不是</br>" + "limitMsg-限制信息（库存不足）</br>"//
			+ "actualPayIntegral-实付积分（单价）</br>" + "actualPayIntegralSum-实付积分小计（实付积分*数量）</br>"//
			+ "integralPrice-积分原价（单价）</br>" + "integralSubtotal-积分小计（积分原价*数量））</br>"//
			+ "订单</br>" + "totalIntegral-总积分</br>" + "useTotalIntegral-使用总积分</br>"//
			+ "isExistIntegralPro-是否存在积分商品1存在，0不存在</br>")
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	@Valids({ @Valid(name = "device", regex = "WAP|PC") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "device", value = "设备/终端", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "buyerId", value = "买家ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<Map<String, Object>> query(@RequestParam String device, @RequestParam Long buyerId, @RequestParam Long sysId) throws Exception {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		try {
			CartParam cartParam = new CartParam();
			cartParam.setDevice(device);
			cartParam.setBuyerId(buyerId);
			cartParam.setSysId(sysId);
			Map<String, Object> map = shoppingCartService.query(cartParam);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("查询成功");
			result.setData(map);
		} catch (CustomException e) {
			result.setCode(e.getCode());
			result.setMessage(e.getMessage());
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	/**
	 * @description : 初始化结算中心页面
	 * @author : wuhaijun
	 * @date : 2017年4月20日 上午11:19:56
	 */
	@ApiOperation(value = "初始化结算中心_value", notes = "初始化结算中心_notes" + "</br>新增返回字段：</br>" //
			+ "商品详情</br>" + "isIntegralPro-是否是积分商品 1是 0不是</br>" + "limitMsg-限制信息（库存不足）</br>"//
			+ "actualPayIntegral-实付积分（单价）</br>" + "actualPayIntegralSum-实付积分小计（实付积分*数量）</br>"//
			+ "integralPrice-积分原价（单价）</br>" + "integralSubtotal-积分小计（积分原价*数量））</br>"//
			+ "订单</br>" + "totalIntegral-总积分</br>" + "useTotalIntegral-使用总积分</br>"//
			+ "remainIntegral-用户积分余额</br>" + "amountToIntegral-积分商品补差价</br>"//
			+ "isExistIntegralPro-是否存在积分商品1存在，0不存在</br>" + "</br>返回码" + "</br>4065-部份商品不符合购物车提交规则")
	@RequestMapping(value = "/initSettle", method = RequestMethod.POST)
	@Valids({ @Valid(name = "device", regex = "WAP|PC") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "buyerId", value = "买家ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "cartIds", value = "购物车ID", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "设备/终端", required = true, dataType = "string", paramType = "query") })
	public Result<Map<String, Object>> initSettle(@RequestParam Long buyerId, @RequestParam Long sysId, @RequestParam String cartIds, @RequestParam String device)
			throws Exception {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		try {
			CartParam cartParam = new CartParam();
			cartParam.setBuyerId(buyerId);
			cartParam.setSysId(sysId);
			cartParam.setCartIds(cartIds);
			cartParam.setDevice(device);
			
			// 查询购物车
			List<ShoppingCart> shoppingCartList = shoppingCartService.queryBySettle(cartIds.split(","), buyerId, sysId);
			Map<String, Object> map = shoppingCartService.settleView(cartParam, shoppingCartList);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("成功");
			result.setData(map);
		} catch (CustomException e) {
			result.setCode(e.getCode());
			result.setMessage(e.getMessage());
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	/**
	 * @description : 结算中心页面
	 * @author : wuhaijun
	 * @date : 2017年4月20日 上午11:19:56
	 */
	@ApiOperation(value = "结算中心_value", notes = "结算中心_notes" + "</br>新增返回字段：</br>" //
			+ "商品详情</br>" + "isIntegralPro-是否是积分商品 1是 0不是</br>" + "limitMsg-限制信息（库存不足）</br>"//
			+ "actualPayIntegral-实付积分（单价）</br>" + "actualPayIntegralSum-实付积分小计（实付积分*数量）</br>"//
			+ "integralPrice-积分原价（单价）</br>" + "integralSubtotal-积分小计（积分原价*数量））</br>"//
			+ "订单</br>" + "totalIntegral-总积分</br>" + "useTotalIntegral-使用总积分</br>"//
			+ "remainIntegral-用户积分余额</br>" + "amountToIntegral-积分商品补差价</br>"//
			+ "isExistIntegralPro-是否存在积分商品1存在，0不存在</br>" + "</br>返回码" + "</br>4064-会员被冻结")
	@RequestMapping(value = "/settle", method = RequestMethod.POST)
	@Valids({ @Valid(name = "device", regex = "WAP|PC"), @Valid(name = "isUseIntegral", maxValue = 1, minValue = 0) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "buyerId", value = "买家ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "cartIds", value = "购物车ID", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "设备/终端", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "receiveAddrId", value = "收货地址ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "payMethodCode", value = "支付方式编码,在线支付:ONLINE,货到付款:CASH_DELIVERY,线下支付:OFFLINE", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "deliveryMethodCode", value = "配送方式编码,申通:ship_sto,顺丰:ship_sf,邮政:ship_ems", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "useRedPacketId", value = "使用红包ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "useStoreBalance", value = "是否使用储值余额", required = false, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "isUseIntegral", value = "是否使用积分抵扣，1是使用，0是不使用", required = false, dataType = "short", paramType = "query"),
			@ApiImplicitParam(name = "useBackBalance", value = "是否使用返现余额", required = false, dataType = "integer", paramType = "query") })
	public Result<Map<String, Object>> settle(@RequestParam Long buyerId, @RequestParam Long sysId, @RequestParam String cartIds, @RequestParam String device,
			@RequestParam(required = false) Long receiveAddrId, @RequestParam String payMethodCode, @RequestParam String deliveryMethodCode,
			@RequestParam(required = false) Long useRedPacketId, @RequestParam(required = false) Integer useStoreBalance, @RequestParam(required = false) Integer useBackBalance,
			@RequestParam(required = false) Short isUseIntegral) throws Exception {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		try {
			// 下单会员状态校验
			if (memberAccountService.filterMemberStatus(buyerId, sysId, result)) {
				return result;
			}
			CartParam cartParam = new CartParam();
			cartParam.setBuyerId(buyerId);
			cartParam.setSysId(sysId);
			cartParam.setCartIds(cartIds);
			cartParam.setDevice(device);
			cartParam.setReceiveAddrId(receiveAddrId);
			cartParam.setPayMethodCode(payMethodCode);
			cartParam.setDeliveryMethodCode(deliveryMethodCode);
			cartParam.setUseRedPacketId(useRedPacketId);
			cartParam.setUseStoreBalance(useStoreBalance);
			cartParam.setUseBackBalance(useBackBalance);
			cartParam.setIsUseIntegral(isUseIntegral == null ? CartParam.IS_USE_INTEGRAL_YES_1 : isUseIntegral);
			// 查询购物车
			List<ShoppingCart> shoppingCartList = shoppingCartService.queryBySettle(cartIds.split(","), buyerId, sysId);
			Map<String, Object> map = shoppingCartService.settle(cartParam, shoppingCartList);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("成功");
			result.setData(map);
		} catch (CustomException e) {
			result.setCode(e.getCode());
			result.setMessage(e.getMessage());
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	/**
	 * @description : 提交订单
	 * @author : wuhaijun
	 * @date : 2017年4月20日 上午11:19:56
	 */
	@ApiOperation(value = "提交订单_value", notes = "提交订单_notes" + "</br>返回码" + "</br>4033-发票信息不完整" + "</br>4064-会员被冻结" + "</br>4062-购买商品未达到最低定购量" + "</br>4063-购买商品超过最高定购量")
	@RequestMapping(value = "/submitOrder", method = RequestMethod.POST)
	@Valids({ @Valid(name = "device", regex = "WAP|PC"), @Valid(name = "invoiceCode", regex = "NOT_INVOICE|ELECTRONIC_INVOICE|ORDINARY_INVOICE|VTA_INVOICE"),
			@Valid(name = "orderRemark", maxLength = 200), @Valid(name = "email", regex = RegexUtils.EMAIL), @Valid(name = "orderRemark", maxLength = 200),
			@Valid(name = "taxpayersNo", regex = RegexUtils.taxpayersNo), @Valid(name = "openAccount", regex = RegexUtils.openAccount),
			@Valid(name = "registerMobilePhone", regex = RegexUtils.registerMobilePhone), @Valid(name = "isUseIntegral", maxValue = 1, minValue = 0) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "buyerId", value = "买家ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "cartIds", value = "购物车ID", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "设备/终端", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "receiveAddrId", value = "收货地址ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "payMethodCode", value = "支付方式编码,在线支付:ONLINE,货到付款:CASH_DELIVERY,线下支付:OFFLINE", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "deliveryMethodCode", value = "配送方式编码,申通:ship_sto,顺丰:ship_sf,邮政:ship_ems", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "useRedPacketId", value = "使用红包ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "useStoreBalance", value = "是否使用储值余额", required = false, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "useBackBalance", value = "是否使用返现余额", required = false, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "orderRemark", value = "订单备注", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "invoiceCode", value = "发票编码,不开发票NOT_INVOICE、电子发票ELECTRONIC_INVOICE、普通发票ORDINARY_INVOICE、增值税发票VTA_INVOICE", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "invoiceTitle", value = "发票抬头 PERSONAL:个人,COMPANY:公司", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "companyName", value = "公司名称", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "email", value = "收票人邮箱", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "taxpayersNo", value = "纳税人识别号", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "registerAddress", value = "公司注册地址", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "registerMobilePhone", value = "注册预留电话", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "openBank", value = "开户银行", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "openAccount", value = "开户账号", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "isUseIntegral", value = "是否使用积分抵扣，1是使用，0是不使用", required = false, dataType = "short", paramType = "query"), })
	public Result<Map<String, Object>> submitOrder(@RequestParam Long buyerId, @RequestParam Long sysId, @RequestParam String cartIds, @RequestParam String device,
			@RequestParam Long receiveAddrId, @RequestParam String payMethodCode, @RequestParam String deliveryMethodCode, @RequestParam(required = false) Long useRedPacketId,
			@RequestParam(required = false) Integer useStoreBalance, @RequestParam(required = false) Integer useBackBalance, @RequestParam(required = false) String orderRemark,
			@RequestParam String invoiceCode, @RequestParam(required = false) String invoiceTitle, @RequestParam(required = false) String companyName,
			@RequestParam(required = false) String email, @RequestParam(required = false) String taxpayersNo, @RequestParam(required = false) String registerAddress,
			@RequestParam(required = false) String registerMobilePhone, @RequestParam(required = false) String openBank, @RequestParam(required = false) String openAccount,
			@RequestParam(required = false) Short isUseIntegral) throws Exception {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		try {
			// 下单会员状态校验
			if (memberAccountService.filterMemberStatus(buyerId, sysId, result)) {
				return result;
			}
			
			CartParam cartParam = new CartParam();
			cartParam.setBuyerId(buyerId);
			cartParam.setSysId(sysId);
			cartParam.setCartIds(cartIds);
			cartParam.setDevice(device);
			cartParam.setReceiveAddrId(receiveAddrId);
			cartParam.setPayMethodCode(payMethodCode);
			cartParam.setDeliveryMethodCode(deliveryMethodCode);
			cartParam.setUseRedPacketId(useRedPacketId);
			cartParam.setUseStoreBalance(useStoreBalance);
			cartParam.setUseBackBalance(useBackBalance);
			cartParam.setOrderRemark(orderRemark);
			cartParam.setInvoiceCode(invoiceCode);
			cartParam.setInvoiceTitle(invoiceTitle);
			cartParam.setCompanyName(companyName);
			cartParam.setEmail(email);
			cartParam.setTaxpayersNo(taxpayersNo);
			cartParam.setRegisterAddress(registerAddress);
			cartParam.setRegisterMobilePhone(registerMobilePhone);
			cartParam.setOpenBank(openBank);
			cartParam.setOpenAccount(openAccount);
			cartParam.setDevice(device);
			cartParam.setIsUseIntegral(isUseIntegral);
			// 查询购物车
			List<ShoppingCart> shoppingCartList = shoppingCartService.queryBySettle(cartIds.split(","), buyerId, sysId);
			Map<String, Object> map = shoppingCartService.submitOrder(cartParam, shoppingCartList);
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setMessage("提交订单成功");
			result.setData(map);
		} catch (CustomException e) {
			result.setCode(e.getCode());
			result.setMessage(e.getMessage());
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	@ApiOperation(value = "获取库存不足不能购买的商品", notes = "获取库存不足不能购买的商品")
	@RequestMapping(value = "/getNotBuy", method = RequestMethod.POST)
	public Result<String> jugeCanBuy(@RequestParam(required = true) Long buyerId, @RequestParam(required = true) Long sysId) throws CustomException {
		String notBuySku = shoppingCartService.jugeCanBuy(buyerId, sysId);
		Result<String> res = new Result<String>();
		res.setData(notBuySku);
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
	/**
	 * 
	 * @author zhuangjiajian
	 * @date 
	 * @param 
	 * @return
	 * @throws CustomException 
	 */
	@ResponseBody
	@RequestMapping(value = "/buyAgain", method = RequestMethod.POST)
	@Valids({ @Valid(name = "frontODetailVo.odetail.buyNum", required = true), @Valid(name = "frontODetailVo.sysId", minValue = 1),
			@Valid(name = "frontODetailVo.device", regex = "WAP|PC") })
	@ApiOperation(notes = "点击 重新购买/确认" + "</br>返回码" + "</br>0-成功" + "</br>4049-全部商品都下架了或者库存为0" + "" + "</br>4050-部分商品下架了或者库存为0" + "</br>库存小于订单里的购买数量时,添加所有库存到购物车里"
			+ "</br>opera=false 只返回code代码和不能加入购物车商品的信息列表" + "</br>确认加入购物车时opera=true,成功加入购物车后返回SUCCESS代码", value = "重新购买")
	public Result<OrderBuyAgainVo> buyAgain(@RequestBody FrontOrderVo frontOrderVo) throws CustomException {
		
		/*
		 * 返回信息  result 
		 * COUNTEMPTY_OR_NOSAL(4049, "all product count is empty or is no sale"), //
		 * PART_ENOUGH(4050, "part of prouduct count is empty or is no sale")//
		 * data:商品标题 商品主图
		 */
		return shoppingCartService.buyAgain(frontOrderVo, frontOrderVo.getOpera());
		
	}
	
	@ApiOperation(value = "收银V4.1-计算运费-wuyicheng", notes = "收银V4.1-计算运费-wuyicheng")
	@RequestMapping(value = "/getFreight", method = RequestMethod.GET)
	@Valids({ @Valid(name = "delivery", regex = "ship_sto|ship_sf|ship_ems"), //
			@Valid(name = "payMethod", regex = "ONLINE|CASH_DELIVERY") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "provinceCode", value = "省编码", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "payMethod", value = "支付方式编码,在线支付:ONLINE,货到付款:CASH_DELIVERY,线下支付:OFFLINE", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "delivery", value = "配送方式编码,申通:ship_sto,顺丰:ship_sf,邮政:ship_ems", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "totalWeight", value = "总重量", required = true, dataType = "bigDecimal", paramType = "query"),
			@ApiImplicitParam(name = "payTotalAmount", value = "支付总金额", required = true, dataType = "bigDecimal", paramType = "query"), })
	public Result<Map<String, BigDecimal>> getFreight(String provinceCode, String delivery, String payMethod, BigDecimal totalWeight, BigDecimal payTotalAmount)
			throws CustomException {
		CartParam cartParam = new CartParam();
		cartParam.setReceiveAddrId(ConstantsUtil.DEFAULT_ADDRESS_ID);
		cartParam.setProvinceCode(provinceCode);
		cartParam.setDedProTotalAmount(payTotalAmount);
		BigDecimal freightPrice = shoppingCartService.getFreight(cartParam, delivery, payMethod, totalWeight);
		Result<Map<String, BigDecimal>> res = new Result<Map<String, BigDecimal>>();
		Map<String, BigDecimal> map = new HashMap<>();
		map.put("freightPrice", freightPrice);
		res.setData(map);
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
}
